//
//  LoginViewController.m
//  JewelryStore
//
//  Created by lcs on 16/10/7.
//  Copyright © 2016年 com. All rights reserved.
//

#import "LoginViewController.h"
#import "CameraViewController.h"

@interface LoginViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIImageView *photo;
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) UIImageView *imageV;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = kWhite;
    _photo = [[UIImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_photo];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    [self openCamera];
    
    
    CameraViewController *cameraVC = [[CameraViewController alloc] init];
    
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1476175823&di=0c6b24cfc92e9dc32b67e391f7a9030b&src=http://imgs.soufun.com/news/2014_12/24/house/1419408695022_000.jpg"]];
//    cameraVC.ringImage = [UIImage imageWithData:imgData];
    cameraVC.ringImage = KImageNamed(@"Ring");

    if (cameraVC.ringImage) {
        [self.navigationController presentViewController:cameraVC animated:YES completion:nil];
    }
    
    
//    [self openPhotoAlbum];
    
}
- (void)openPhotoAlbum
{
    UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
        
    }
    pickerImage.delegate = self;
    pickerImage.allowsEditing = NO;
    [self.navigationController presentViewController:pickerImage animated:YES completion:nil];
}
- (void)openCamera {
    // UIImagePickerControllerCameraDeviceRear 后置摄像头
    // UIImagePickerControllerCameraDeviceFront 前置摄像头
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!isCamera) {
        NSLog(@"没有摄像头");
        return ;
    }
    
    _imagePicker = [[UIImagePickerController alloc] init];
    UIButton *button = [[UIButton alloc] init];
    button.center = _imagePicker.view.center;
    button.frame = CGRectMake(button.frame.origin.x-50, SCREEN_HEIGHT-80, 100, 100);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [_imagePicker.view addSubview:button];
    
    
    _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    _imagePicker.delegate = self;
    // 编辑模式
    _imagePicker.allowsEditing = YES;
    
    [self  presentViewController:_imagePicker animated:YES completion:^{
        TT_Log(@"11111111111");
    }];
    
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    TT_Log(@"%@", info);
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [_photo setImage:image];
    
//    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)buttonClick
{
    [_imagePicker takePicture];
}


@end
